package com.cloud.base.controller;

import com.cloud.api.constants.StrVo;
import com.cloud.base.domain.city.City;
import com.cloud.base.domain.city.District;
import com.cloud.base.domain.city.Letter2City;
import com.cloud.base.domain.city.Province;
import com.cloud.base.service.IProvinceCityService;
import com.cloud.core.util.R;
import com.cloud.log.annotation.Log;
import com.cloud.log.enums.BusinessType;
import com.cloud.log.enums.OperatorType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dhaigang
 * @since 2023-07-03
 */
@RestController
@Validated
@RequestMapping("/province-city")
public class ProvinceCityController {
    @Resource
    private IProvinceCityService provinceCityService;
    @Tag(name = "v1.0")
    @Operation(summary = "获取所有省份(管理)")
    @PostMapping("/getAllProvince")
    @Log(title = "地理位置管理-获取所有省份", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<Province> getAllProvince( ) throws IOException {
        return  R.ok(provinceCityService.getProvinceFromRedis());
    }


    @Tag(name = "v1.0")
    @Operation(summary = "通过省级代码获取所有城市(管理)")
    @PostMapping("/getCitysByProvinceCode")
    @Log(title = "地理位置管理-通过省级代码获取所有城市", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<City> getCitysByProvinceCode(@RequestBody @Valid StrVo para) throws IOException {
        return  R.ok(provinceCityService.getCitysByProvinceCode(para.getId()));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "通过省级名称获取所有城市(管理)")
    @PostMapping("/getCitysByProvinceName")
    @Log(title = "地理位置管理-通过省级名称获取所有城市", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<City> getCitysByProvinceName(@RequestBody @Valid Province para) throws IOException {
        return  R.ok(provinceCityService.getCitysByProvinceName(para.getProvinceName()));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "根据城市名称获取地区信息")
    @PostMapping("/getDistrictByCity")
    @Log(title = "地理位置管理-根据城市名称获取地区信息", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<District> selectDistrictByCity(@RequestBody @Valid City para) throws IOException {
        return R.ok(provinceCityService.selectDistrictByCity(para.getCityName()));
    }


    @Tag(name = "v1.0")
    @Operation(summary = "城市列表(h5)")
    @PostMapping("/getLetter2City")
    @Log(title = "地理位置管理-城市列表", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<Letter2City> getLetter2City() throws IOException {
        return  R.ok(provinceCityService.getLetter2City());
    }
}
